

<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
  <meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />

  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>usbh_net &mdash; CherryUSB 1.4.2 documentation</title>
      <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
      <link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=e59714d7" />

  
      <script src="../_static/jquery.js?v=5d32c60e"></script>
      <script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
      <script src="../_static/documentation_options.js?v=02f2166e"></script>
      <script src="../_static/doctools.js?v=9a2dae69"></script>
      <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
    <script src="../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="next" title="usbh_bluetooth" href="usbh_bluetooth.html" />
    <link rel="prev" title="usbh_msc" href="usbh_msc.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >

          
          
          <a href="../index.html" class="icon icon-home">
            CherryUSB
          </a>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <p class="caption" role="heading"><span class="caption-text">快速上手</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../quick_start/start.html">入门必看</a></li>
<li class="toctree-l1"><a class="reference internal" href="../quick_start/demo.html">基于现有 demo 快速验证</a></li>
<li class="toctree-l1"><a class="reference internal" href="../quick_start/transplant.html">芯片通用移植指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../quick_start/rtthread.html">基于 RT-Thread 软件包开发指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../quick_start/esp.html">基于 ESP-Registry 开发指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../q%26a.html">Q &amp; A</a></li>
<li class="toctree-l1"><a class="reference internal" href="../opensource.html">官方开源项目分享</a></li>
<li class="toctree-l1"><a class="reference internal" href="../share.html">开发者经验/开源项目分享</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">USB 基本知识点</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb2.0_basic.html">USB 基本概念(2.0 为主)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb3.0_basic.html">USB 基本概念(3.0 为主)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb_desc.html">USB 描述符</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb_request.html">USB 设备请求</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb_enum.html">USB 枚举</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usb/usb_ext.html">USB 知识点拓展</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">API 手册</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../api/api_device.html">设备协议栈</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/api_host.html">主机协议栈</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/api_port.html">主从驱动</a></li>
<li class="toctree-l1"><a class="reference internal" href="../api/api_config.html">USB CONFIG 说明</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Class 指南</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../class/class_cdc.html">CDC</a></li>
<li class="toctree-l1"><a class="reference internal" href="../class/class_hid.html">HID</a></li>
<li class="toctree-l1"><a class="reference internal" href="../class/class_msc.html">MSC</a></li>
<li class="toctree-l1"><a class="reference internal" href="../class/class_audio.html">UAC</a></li>
<li class="toctree-l1"><a class="reference internal" href="../class/class_video.html">UVC</a></li>
<li class="toctree-l1"><a class="reference internal" href="../class/winusb.html">WINUSB</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">例程说明</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="usbd_cdc_acm.html">usbd_cdc_acm</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbd_hid.html">usbd_hid</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbd_msc.html">usbd_msc</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbd_rndis.html">usbd_rndis</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbd_ecm.html">usbd_cdc_ecm</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbd_audiov1.html">usbd_audiov1</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbd_audiov2.html">usbd_audiov2</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbd_video.html">usbd_video</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbd_winusb.html">usbd_winusb</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbd_webusb.html">usbd_webusb</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbh_serial.html">usbh_serial</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbh_hid.html">usbh_hid</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbh_msc.html">usbh_msc</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">usbh_net</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbh_bluetooth.html">usbh_bluetooth</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbh_wifi.html">usbh_wifi</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbd_vendor.html">vendor device 驱动编写</a></li>
<li class="toctree-l1"><a class="reference internal" href="usbh_vendor.html">vendor host 驱动编写</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">USBIP 介绍</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../usbip/ohci.html">OHCI</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/ehci.html">EHCI</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/xhci.html">XHCI</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/chipidea.html">CHIPIDEA</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/dwc2.html">DWC2</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/musb.html">MUSB</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/fotg210.html">FOTG210</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/cdns2.html">CDNS2</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/cdns3.html">CDNS3</a></li>
<li class="toctree-l1"><a class="reference internal" href="../usbip/dwc3.html">DWC3</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">工具使用</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html">chryusb_configurator</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html#usb-protocol-suite">力科 USB Protocol Suite</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html#wireshark">Wireshark</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tools/index.html#audacity">Audacity</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">版本说明</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../version.html">版本说明</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">商业支持</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../support/index.html">商业支持</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">CherryUSB</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
      <li class="breadcrumb-item active">usbh_net</li>
      <li class="wy-breadcrumbs-aside">
            <a href="../_sources/demo/usbh_net.rst.txt" rel="nofollow"> View page source</a>
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <section id="usbh-net">
<h1>usbh_net<a class="headerlink" href="#usbh-net" title="Link to this heading"></a></h1>
<p>本节主要介绍 USB 网卡的使用，USB 网卡推荐采用 AIR780(RNDIS)，EC20(ECM/RNDIS), 手机（RNDIS）,RTL8152 USB 网卡，AX88772 USB 网卡。</p>
<p>USB 网卡传输层面已经对接好了 LWIP 的收发接口，因此，用户只需要包含 <strong>platform/XXX/usbh_lwip.c</strong> 并根据需要开启对应的网卡类的宏即可。</p>
<ul class="simple">
<li><p>当前支持以下网卡类：</p></li>
</ul>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="c1">// #define CONFIG_USBHOST_PLATFORM_CDC_ECM</span>
<span class="c1">// #define CONFIG_USBHOST_PLATFORM_CDC_RNDIS</span>
<span class="c1">// #define CONFIG_USBHOST_PLATFORM_CDC_NCM</span>
<span class="c1">// #define CONFIG_USBHOST_PLATFORM_ASIX</span>
<span class="c1">// #define CONFIG_USBHOST_PLATFORM_RTL8152</span>
<span class="c1">// #define CONFIG_USBHOST_PLATFORM_BL616</span>
</pre></div>
</div>
<ul class="simple">
<li><p>包含了对接 LWIP 的输入输出接口，举例如下</p></li>
</ul>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">err_t</span><span class="w"> </span><span class="nf">usbh_cdc_ecm_linkoutput</span><span class="p">(</span><span class="k">struct</span><span class="w"> </span><span class="nc">netif</span><span class="w"> </span><span class="o">*</span><span class="n">netif</span><span class="p">,</span><span class="w"> </span><span class="k">struct</span><span class="w"> </span><span class="nc">pbuf</span><span class="w"> </span><span class="o">*</span><span class="n">p</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">ret</span><span class="p">;</span>
<span class="w">    </span><span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">netif</span><span class="p">;</span>

<span class="w">    </span><span class="n">usbh_lwip_eth_output_common</span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="w"> </span><span class="n">usbh_cdc_ecm_get_eth_txbuf</span><span class="p">());</span>
<span class="w">    </span><span class="n">ret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">usbh_cdc_ecm_eth_output</span><span class="p">(</span><span class="n">p</span><span class="o">-&gt;</span><span class="n">tot_len</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">ret</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="n">ERR_BUF</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="n">ERR_OK</span><span class="p">;</span>
<span class="w">    </span><span class="p">}</span>
<span class="p">}</span>

<span class="kt">void</span><span class="w"> </span><span class="nf">usbh_cdc_ecm_eth_input</span><span class="p">(</span><span class="kt">uint8_t</span><span class="w"> </span><span class="o">*</span><span class="n">buf</span><span class="p">,</span><span class="w"> </span><span class="kt">uint32_t</span><span class="w"> </span><span class="n">buflen</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="n">usbh_lwip_eth_input_common</span><span class="p">(</span><span class="o">&amp;</span><span class="n">g_cdc_ecm_netif</span><span class="p">,</span><span class="w"> </span><span class="n">buf</span><span class="p">,</span><span class="w"> </span><span class="n">buflen</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<ul class="simple">
<li><p>网卡类枚举完成后，注册 netif，并且创建网卡接收线程（因此使用 RTTHREAD 时不需要使用 RTT 的接收线程模块）。</p></li>
<li><p>必须开启 DHCP client 服务，用于从 USB 网卡获取 IP 地址。</p></li>
</ul>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="kt">void</span><span class="w"> </span><span class="nf">usbh_cdc_ecm_run</span><span class="p">(</span><span class="k">struct</span><span class="w"> </span><span class="nc">usbh_cdc_ecm</span><span class="w"> </span><span class="o">*</span><span class="n">cdc_ecm_class</span><span class="p">)</span>
<span class="p">{</span>
<span class="w">    </span><span class="k">struct</span><span class="w"> </span><span class="nc">netif</span><span class="w"> </span><span class="o">*</span><span class="n">netif</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">g_cdc_ecm_netif</span><span class="p">;</span>

<span class="w">    </span><span class="n">netif</span><span class="o">-&gt;</span><span class="n">hwaddr_len</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">6</span><span class="p">;</span>
<span class="w">    </span><span class="n">memcpy</span><span class="p">(</span><span class="n">netif</span><span class="o">-&gt;</span><span class="n">hwaddr</span><span class="p">,</span><span class="w"> </span><span class="n">cdc_ecm_class</span><span class="o">-&gt;</span><span class="n">mac</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">);</span>

<span class="w">    </span><span class="n">IP4_ADDR</span><span class="p">(</span><span class="o">&amp;</span><span class="n">g_ipaddr</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">IP4_ADDR</span><span class="p">(</span><span class="o">&amp;</span><span class="n">g_netmask</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>
<span class="w">    </span><span class="n">IP4_ADDR</span><span class="p">(</span><span class="o">&amp;</span><span class="n">g_gateway</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span>

<span class="w">    </span><span class="n">netif</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">netif_add</span><span class="p">(</span><span class="n">netif</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">g_ipaddr</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">g_netmask</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">g_gateway</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">usbh_cdc_ecm_if_init</span><span class="p">,</span><span class="w"> </span><span class="n">tcpip_input</span><span class="p">);</span>
<span class="w">    </span><span class="n">netif_set_default</span><span class="p">(</span><span class="n">netif</span><span class="p">);</span>
<span class="w">    </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="n">netif_is_up</span><span class="p">(</span><span class="n">netif</span><span class="p">))</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="n">dhcp_handle</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">usb_osal_timer_create</span><span class="p">(</span><span class="s">&quot;dhcp&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">200</span><span class="p">,</span><span class="w"> </span><span class="n">dhcp_timeout</span><span class="p">,</span><span class="w"> </span><span class="n">netif</span><span class="p">,</span><span class="w"> </span><span class="nb">true</span><span class="p">);</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">dhcp_handle</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="n">USB_LOG_ERR</span><span class="p">(</span><span class="s">&quot;timer creation failed! </span><span class="se">\r\n</span><span class="s">&quot;</span><span class="p">);</span>
<span class="w">        </span><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w">        </span><span class="p">}</span>
<span class="w">    </span><span class="p">}</span>

<span class="w">    </span><span class="n">usb_osal_thread_create</span><span class="p">(</span><span class="s">&quot;usbh_cdc_ecm_rx&quot;</span><span class="p">,</span><span class="w"> </span><span class="mi">2048</span><span class="p">,</span><span class="w"> </span><span class="n">CONFIG_USBHOST_PSC_PRIO</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">usbh_cdc_ecm_rx_thread</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">);</span>
<span class="cp">#if LWIP_DHCP</span>
<span class="w">    </span><span class="n">dhcp_start</span><span class="p">(</span><span class="n">netif</span><span class="p">);</span>
<span class="w">    </span><span class="n">usb_osal_timer_start</span><span class="p">(</span><span class="n">dhcp_handle</span><span class="p">);</span>
<span class="cp">#endif</span>
<span class="p">}</span>
</pre></div>
</div>
<ul class="simple">
<li><p>获取到 IP 以后，就与 USB 没有关系了，直接使用 LWIP 的接口即可。</p></li>
<li><p>需要注意以下参数</p></li>
</ul>
<p>LWIP_TCPIP_CORE_LOCKING_INPUT 用于不使用 lwip 内置的 tcpip 线程，而使用 USB 自己的处理线程。</p>
<p>LWIP_TCPIP_CORE_LOCKING 在现在 lwip 版本中默认是打开的，也推荐必须打开。</p>
<p>PBUF_POOL_BUFSIZE 推荐大于1600，搭配 LWIP_TCPIP_CORE_LOCKING_INPUT 使用，因为我们提供了使用 zero mempy 的方式，使用静态 pbuf，而不是把数据 copy 到 pbuf 中。</p>
<p>TCPIP_THREAD_STACKSIZE 推荐大于 1K，防止栈溢出。</p>
<div class="highlight-C notranslate"><div class="highlight"><pre><span></span><span class="cp">#if LWIP_TCPIP_CORE_LOCKING_INPUT != 1</span>
<span class="cp">#warning suggest you to set LWIP_TCPIP_CORE_LOCKING_INPUT to 1, usb handles eth input with own thread</span>
<span class="cp">#endif</span>

<span class="cp">#if LWIP_TCPIP_CORE_LOCKING != 1</span>
<span class="cp">#error must set LWIP_TCPIP_CORE_LOCKING to 1</span>
<span class="cp">#endif</span>

<span class="cp">#if PBUF_POOL_BUFSIZE &lt; 1600</span>
<span class="cp">#error PBUF_POOL_BUFSIZE must be larger than 1600</span>
<span class="cp">#endif</span>

<span class="cp">#if TCPIP_THREAD_STACKSIZE &lt; 1024</span>
<span class="cp">#error TCPIP_THREAD_STACKSIZE must be &gt;= 1024</span>
<span class="cp">#endif</span>
</pre></div>
</div>
</section>


           </div>
          </div>
          <footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
        <a href="usbh_msc.html" class="btn btn-neutral float-left" title="usbh_msc" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
        <a href="usbh_bluetooth.html" class="btn btn-neutral float-right" title="usbh_bluetooth" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2022 ~ 2024, sakumisu.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>